home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_kdelibs.idb / usr / freeware / kde / include / kfiledialog.h.z / kfiledialog.h
Encoding:
C/C++ Source or Header  |  1999-01-26  |  16.0 KB  |  551 lines

  1. // -*- c++ -*-
  2. /* This file is part of the KDE libraries
  3.     Copyright (C) 1997, 1998 Richard Moore <rich@kde.org>
  4.                   1998 Stephan Kulow <coolo@kde.org>
  5.                   1998 Daniel Grana <grana@ie.iwi.unibe.ch>
  6.     
  7.     This library is free software; you can redistribute it and/or
  8.     modify it under the terms of the GNU Library General Public
  9.     License as published by the Free Software Foundation; either
  10.     version 2 of the License, or (at your option) any later version.
  11.  
  12.     This library is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.     Library General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU Library General Public License
  18.     along with this library; see the file COPYING.LIB.  If not, write to
  19.     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20.     Boston, MA 02111-1307, USA.
  21. */
  22.  
  23. #ifndef __KFILEDIALOG_H__
  24. #define __KFILEDIALOG_H__
  25.  
  26. #include <qdialog.h>
  27. #include <qstring.h>
  28. #include <qstack.h>
  29. #include <qstrlist.h>
  30. #include <qpixmap.h>
  31.  
  32. #include "kfileinfo.h"
  33.  
  34. class KToolBar;
  35. class KDirListBox;
  36. class KFileInfoContents;
  37. class QPopupMenu;
  38. class QCheckBox;
  39. class QComboBox;
  40. class KCombo;
  41. class KBookmarkManager;
  42. class KBookmark;
  43. class QStrIList;
  44. class QLineEdit;
  45. class KDir;
  46. class QLabel;
  47. class QVBoxLayout;
  48. class QGridLayout;
  49. class QHBoxLayout;
  50. class KFileFilter;
  51.  
  52. /**
  53.  * The KFileDialog widget provides a user (and developer) friendly way to
  54.  * select files. The widget can be used as a drop in replacement for the
  55.  * QFileDialog widget, but has greater functionality and a nicer GUI.
  56.  *
  57.  * You will usually want to use one of the two static methods
  58.  * KFileDialog::getOpenFileName or KFileDialog::getCloseFileName.
  59.  *
  60.  * The dialog has been designed to allow applications to customise it
  61.  * by subclassing. It uses geometry management to ensure that subclasses
  62.  * can easily add children which will be incorperated into the layout.
  63.  *
  64.  * @short A file selection dialog
  65.  *
  66.  * @author Richard J. Moore rich@kde.org
  67.  * @version $Id: kfiledialog.h,v 1.15 1998/06/16 07:53:04 garbanzo Exp $
  68.  */
  69. class KFileBaseDialog : public QDialog
  70. {
  71.     Q_OBJECT
  72.     
  73. public:
  74.     /**
  75.       * Construct a KFileBaseDialog
  76.       *
  77.       * @param dirName  The name of the directory to start in.
  78.       * @param filter   A shell glob that specifies which files to display.
  79.       * see setFilter for details on how to use this argument
  80.       * @param acceptURLs If set to false, kfiledialog will just accept
  81.       * files on the local filesystem.
  82.       */
  83.     KFileBaseDialog(const char *dirName, const char *filter= 0,
  84.         QWidget *parent= 0, const char *name= 0, 
  85.         bool modal = false, bool acceptURLs = true);
  86.     
  87.     
  88.     /**
  89.       * Cleans up
  90.       */
  91.     ~KFileBaseDialog();
  92.     
  93.     /**
  94.       * Returns the fully qualified filename.
  95.       */
  96.     QString selectedFile();
  97.     
  98.     /**
  99.       * Return the path of the selected directory.
  100.       */
  101.     QString dirPath();
  102.     
  103.     /**
  104.       * Rereads the currently selected directory.
  105.       */
  106.     void rereadDir();
  107.     
  108.     /**
  109.       * Go back to the previous directory if this is not the first.
  110.       */
  111.     void back();
  112.     
  113.     /**
  114.       * Go forward to the next directory if this is not the last.
  115.       */
  116.     void forward();
  117.     
  118.     /**
  119.       * Go home.
  120.       */
  121.     void home();
  122.     
  123.     /**
  124.       * Go to parent.
  125.       */
  126.     void cdUp();
  127.  
  128.     /**
  129.       * Returns true for local files, false for remote files.
  130.       */
  131.     bool dirIsLocal() const { return !acceptUrls; }
  132.     
  133.     /**
  134.       * Returns the url of the selected filename
  135.       */
  136.     QString selectedFileURL();
  137.     
  138.     /**
  139.       * Returns the URLs of the selected files.
  140.       */
  141.     QStrList selectedFileURLList();
  142.     
  143.     /**
  144.       * If the argument is true the dialog will accept multiple selections.
  145.       */
  146.     void setMultiSelection(bool multi= true);
  147.     
  148.     /**
  149.       * Returns true if multiple selections are enabled.
  150.       */
  151.     bool isMultiSelection() const { return false; }
  152.     
  153.     /**
  154.       * This method creates a modal directory dialog and returns the selected 
  155.       * directory or an empty string if none was chosen. Note that with 
  156.       * this method the user must select an existing directory.
  157.       *
  158.       * @param url This specifies the path the dialog will start in.
  159.       * @param parent The widget the dialog will be centered on initially.
  160.       * @param name The name of the dialog widget.
  161.       */
  162.     static QString getDirectory(const char *url, QWidget *parent = 0,  
  163.                 const char *name = 0);
  164.  
  165.     /**
  166.       * This a multiple selection version of getOpenFileURL().
  167.       */
  168.     QStrList getOpenFileURLList(const char *url= 0,
  169.                 const char *filter= 0,
  170.                 QWidget *parent= 0,
  171.                 const char *name= 0);
  172.     
  173.     /**
  174.       * This a multiple selection version of getSaveFileURL().
  175.       */
  176.     QStrList getSaveFileURLList(const char *url= 0, const char *filter= 0,
  177.                 QWidget *parent= 0, const char *name= 0);
  178.     
  179.     /**
  180.       * Sets the directory to view 
  181.       * @param name URL to show
  182.       */
  183.     void setDir(const char *name) { setDir(name, true); }
  184.  
  185.     /**
  186.      * Sets the filename to preselect. 
  187.      * It takes absolute and relative file names
  188.      */
  189.     void setSelection(const char *name);
  190.  
  191.     /**
  192.      * Sets the filter to be used to filter. You can set more
  193.      * filters for the user to select seperated by \n. Every
  194.      * filter entry is defined through namefilter|text to diplay.
  195.      * If no | is found in the expression, just the namefilter is
  196.      * shown. Examples:
  197.      *
  198.      * <pre>
  199.      * kfile->setFilter("*.cpp|C++ Source Files\n*.h|Header files");
  200.      * kfile->setFilter("*.cpp");
  201.      * kfile->setFilter("*.cpp|Sources (*.cpp");
  202.      * </pre>
  203.      *
  204.      * Note: the text to display is not parsed in any way. So, if you
  205.      * want to show the suffix to select by a specific filter, you must
  206.      * repeat it.
  207.      */
  208.     void setFilter(const char *filter);
  209.  
  210. signals:
  211.     /**
  212.       * Emitted when the user selects a file.
  213.       */
  214.     void fileSelected(const char *);
  215.     
  216.     /**
  217.       * Emitted when the user highlights a file.
  218.       */
  219.     void fileHighlighted(const char *);
  220.     
  221.     /**
  222.       * Emitted when a new directory is entered.
  223.       */
  224.     void dirEntered(const char *);
  225.     
  226.     /**
  227.       * Emitted when the allowable history operations change.
  228.       */
  229.     void historyUpdate(bool, bool);
  230.  
  231. protected:
  232.     KToolBar *toolbar;
  233.     KFileInfoContents *fileList;
  234.     
  235.     QStrIList *visitedDirs;  // to fill the combo box
  236.     static QString *lastDirectory;
  237.  
  238.     QPopupMenu *bookmarksMenu; 
  239.     QCheckBox *hiddenToggle;
  240.     KCombo *locationEdit;
  241.  
  242.     KFileFilter *filterWidget;
  243.  
  244.     KBookmarkManager *bookmarks;
  245.     QStrList history;
  246.     KDir *dir;
  247.     
  248.     QLabel *myStatusLine;
  249.  
  250.     // the last selected filename
  251.     QString filename_;
  252.     // the name of the filename set by setSelection
  253.     QString selection;
  254.  
  255.     // represents the check box. Initialized by "ShowHidden"
  256.     bool showHidden;
  257.  
  258.     /*
  259.      * indicates, if the status bar should be shown. 
  260.      * Initialized by "ShowStatusLine"
  261.      */ 
  262.     bool showStatusLine;
  263.     bool showFilter;
  264.     bool acceptUrls;
  265.  
  266.      /**
  267.       * Contains all URLs you can reach with the back button.
  268.       */
  269.     QStack<QString> backStack;
  270.     
  271.     /**
  272.       * Contains all URLs you can reach with the forward button.
  273.       */
  274.     QStack<QString> forwardStack;
  275.     
  276.     /**
  277.       * Lock @ref #backStack and @ref #forwardStack .
  278.       */
  279.     bool stackLock;
  280.     
  281.     void resizeEvent(QResizeEvent *);
  282.     
  283.     /**
  284.       * Subclasses should reimplement this method to swallow the main
  285.       * layout. This allows the addition of children that are outside
  286.       * the existing layout. The function should return the widget that
  287.       * should be the parent of the main layout.
  288.       */
  289.     virtual QWidget *swallower() { return this; }
  290.  
  291.     /**
  292.       * Subclasses, that want another view should override this 
  293.       * function to provide another file view.
  294.       * It will be used to display files.
  295.       **/
  296.     virtual KFileInfoContents *initFileList( QWidget *parent ) = 0;
  297.  
  298.     /**
  299.       * Overload this function, if you want the filter shown/unshown
  300.       */
  301.     virtual bool getShowFilter() = 0;
  302.     
  303.     /**
  304.       * Set the directory to view 
  305.       * @param name URL to show
  306.       * @param clearforward indicate, if the forward queue 
  307.       * should be cleared
  308.       */
  309.     void setDir(const char *name, bool clearforward = true);
  310.  
  311.     /**
  312.       * adds a entry of the current directory. If disableUpdating is set
  313.       * to true, it will care about clever updating
  314.       **/
  315.     void addDirEntry(KFileInfo *entry, bool disableUpdating);
  316.  
  317.     /**
  318.       * rebuild geometry managment. 
  319.       *
  320.       */
  321.     void initGUI();
  322.  
  323.     /**
  324.       * Makes a new directory in current directory after asking user
  325.       * for a name
  326.       */
  327.     void mkdir();
  328.  
  329.     /**
  330.       * takes action on the new location. If it's a directory, change
  331.       * into it, if it's a file, correct the name, etc.
  332.       * @param takeFiles if set to true, if will close the dialog, if
  333.       * txt is a file name
  334.       */
  335.     void checkPath(const char *txt, bool takeFiles = false);
  336.  
  337.     /**
  338.       * this functions must be called by the constructor of a derived
  339.       * class. 
  340.       **/
  341.     void init();
  342.  
  343. protected slots:
  344.     void pathChanged();
  345.     void comboActivated(int);
  346.     void toolbarCallback(int);
  347.     void toolbarPressedCallback(int);
  348.     void dirActivated(KFileInfo*);
  349.     void fileActivated(KFileInfo*);
  350.     void fileHighlighted(KFileInfo*);
  351.     void updateHistory(bool, bool);
  352.     void filterChanged();
  353.     void locationChanged(const char*);
  354.     
  355.     void setHiddenToggle(bool);
  356.     void slotDirEntry(KFileInfo *);
  357.     void slotFinished();
  358.     void slotKfmError(int, const char *);
  359.     void insertNewFiles(const KFileInfoList *newone);
  360.     void completion();
  361.     virtual void updateStatusLine();
  362.     virtual void okPressed();
  363.  
  364.     /**
  365.       * You should override this method if you change the user interface of
  366.       * this dialog in a subclass in order to invoke your updated user help.
  367.       */
  368.     virtual void help();
  369.     
  370.     /**
  371.       * Add the current location to the global bookmarks list
  372.       */
  373.     void addToBookmarks();
  374.     void bookmarksChanged();
  375.     void fillBookmarkMenu( KBookmark *parent, QPopupMenu *menu, int &id );
  376.  
  377. private:
  378.     //
  379.     // the father of the widget. By default itself, but
  380.     // to make it customable, this can be overriden by 
  381.     // overriding swallower()
  382.     //
  383.     QWidget *wrapper;
  384.   
  385.     // flag for perfomance hype ;)
  386.     bool repaint_files; 
  387.     // for the handling of the cursor
  388.     bool finished;
  389.     
  390.     // indicates, if the file selector accepts just existing
  391.     // files or not. If set to true, it will check for local
  392.     // files, if they exist
  393.     bool acceptOnlyExisting;
  394.  
  395.     // now following all kind of widgets, that I need to rebuild
  396.     // the geometry managment
  397.     QVBoxLayout *boxLayout;
  398.     QGridLayout *lafBox;
  399.     QHBoxLayout *btngroup;
  400.     QPushButton *bOk, *bCancel, *bHelp;
  401.     QLabel *locationLabel, *filterLabel;
  402.     QString filterString;
  403. };
  404.  
  405. /**
  406.  * The KDirDialog widget provides a user (and developer) friendly way to
  407.  * select directories. It is a specalised KFileDialog.
  408.  *
  409.  * You will usually want to use one of the two static methods
  410.  * KFileDialog::getDirectory. 
  411.  *
  412.  * Being a subclass of KFileKFileBaseDialog, it can therefore be easily
  413.  * customised.
  414.  *
  415.  * @short A directory selection dialog
  416.  *
  417.  * @version $Id: kfiledialog.h,v 1.15 1998/06/16 07:53:04 garbanzo Exp $
  418.  */
  419. class KDirDialog : public KFileBaseDialog
  420. {
  421. public:
  422.     
  423.     KDirDialog(const char *url, QWidget *parent, const char *name);
  424.     
  425. protected:
  426.     virtual KFileInfoContents *initFileList( QWidget *parent );
  427.     virtual bool getShowFilter() { return false; }
  428.     virtual void updateStatusLine();
  429. };
  430.  
  431. class KFileDialog : public KFileBaseDialog
  432. {
  433. public: 
  434.     KFileDialog(const char *dirName, const char *filter= 0,
  435.         QWidget *parent= 0, const char *name= 0, 
  436.         bool modal = false, bool acceptURLs = true);
  437.  
  438.     /**
  439.       * This method creates a modal file dialog and returns the selected 
  440.       * filename or an empty string if none was chosen. Note that with 
  441.       * this method the user must select an existing filename.
  442.       *
  443.       * @param dir This specifies the path the dialog will start in.
  444.       * @param filter This is a space seperated list of shell globs.
  445.       * @param parent The widget the dialog will be centered on initially.
  446.       * @param name The name of the dialog widget.
  447.       */
  448.     static QString getOpenFileName(const char *dir= 0, const char *filter= 0,
  449.                    QWidget *parent= 0, const char *name= 0);
  450.  
  451.     /**
  452.      * This method creates a modal file dialog and returns the selected 
  453.      * filename or an empty string if none was chosen. Note that with this 
  454.      * method the user need not select an existing filename.
  455.      *
  456.      * @param dir This specifies the path the dialog will start in.
  457.      * @param filter This is a space seperated list of shell globs.
  458.      * @param parent The widget the dialog will be centered on initially.
  459.      * @param name The name of the dialog widget.
  460.      */
  461.     static QString getSaveFileName(const char *dir= 0, const char *filter= 0,
  462.                    QWidget *parent= 0, const char *name= 0);
  463.  
  464.     /**
  465.      * This function is similar to getOpenFileName() but allows the
  466.      * user to select a local file or a remote file.
  467.      */
  468.     static QString getOpenFileURL(const char *url= 0, const char *filter= 0,
  469.                   QWidget *parent= 0, const char *name= 0);
  470.     
  471.     /**
  472.      * This function is similar to getSaveFileName() but allows the
  473.      * user to select a local file or a remote file.
  474.      */
  475.     static QString getSaveFileURL(const char *url= 0, const char *filter= 0,
  476.                   QWidget *parent= 0, const char *name= 0);
  477.     
  478. protected:
  479.     virtual KFileInfoContents *initFileList( QWidget *parent);
  480.     virtual bool getShowFilter();
  481. };
  482.  
  483.  
  484. typedef bool (*PreviewHandler)( const KFileInfo *, const QString fileName,
  485.                                        QString &, QPixmap & );
  486.  
  487. /**
  488.   * Preview modules are of one of these types, which means their preview is either
  489.   * text or an pixmap (image).
  490.   */
  491. enum PreviewType { PreviewText = 0x001,
  492.                    PreviewPixmap = 0x002 };
  493.  
  494.  
  495. /**
  496.  * The KFilePreviewDialog widget provides a user (and developer) friendly way to
  497.  * select files while showing a preview.
  498.  *
  499.  * You will usually want to use one of the static methods
  500.  * KFilePreviewDialog::get[Open|Save][Name|URL](...) which are the same
  501.  * as the KFileDialog ones. 
  502.  *
  503.  * Being a subclass of KFileKFileBaseDialog, it can therefore be easily
  504.  * customised.
  505.  *
  506.  * @short A file selection dialog with preview
  507.  *
  508.  * @version $Id: kfiledialog.h,v 1.15 1998/06/16 07:53:04 garbanzo Exp $
  509.  */
  510. class KFilePreviewDialog : public KFileBaseDialog
  511. {
  512. public:
  513.     
  514.     KFilePreviewDialog(const char *dirName, const char *filter= 0,
  515.                QWidget *parent= 0, const char *name= 0, 
  516.                bool modal = false, bool acceptURLs = true);
  517.     ~KFilePreviewDialog() {}
  518.  
  519.     static QString getOpenFileName(const char *dir= 0, const char *filter= 0,
  520.                    QWidget *parent= 0, const char *name= 0);
  521.     static QString getSaveFileName(const char *dir= 0, const char *filter= 0,
  522.                    QWidget *parent= 0, const char *name= 0);
  523.     static QString getOpenFileURL(const char *url= 0, const char *filter= 0,
  524.                   QWidget *parent= 0, const char *name= 0);
  525.     static QString getSaveFileURL(const char *url= 0, const char *filter= 0,
  526.                   QWidget *parent= 0, const char *name= 0);
  527.  
  528.     /**
  529.       * This is a static method which allow a user to define a new preview dialog module.
  530.       *
  531.       * @param format This identifies the module e.g. "JPG" or "PNG".
  532.       * @param readPreview This is the function which generates the preview
  533.       * @param type This is the type of the preview module
  534.       * @see PreviewType
  535.       */
  536.     static void registerPreviewModule( const char * format, PreviewHandler readPreview,
  537.                                 PreviewType type);
  538.  
  539.     
  540. protected:
  541.     virtual KFileInfoContents *initFileList( QWidget *parent );
  542.     virtual bool getShowFilter();
  543. };
  544.  
  545. #endif
  546.  
  547.  
  548.  
  549.  
  550.  
  551.